package it.stasbranger.myproject.service;

import it.stasbranger.myproject.dao.SuppliersDAO;
import it.stasbranger.myproject.dao.SuppliersHasRequestsDAO;

import it.stasbranger.myproject.domain.Suppliers;
import it.stasbranger.myproject.domain.SuppliersHasRequests;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Suppliers entities
 * 
 */

@Service("SuppliersService")
@Transactional
public class SuppliersServiceImpl implements SuppliersService {

	/**
	 * DAO injected by Spring that manages Suppliers entities
	 * 
	 */
	@Autowired
	private SuppliersDAO suppliersDAO;

	/**
	 * DAO injected by Spring that manages SuppliersHasRequests entities
	 * 
	 */
	@Autowired
	private SuppliersHasRequestsDAO suppliersHasRequestsDAO;

	/**
	 * Instantiates a new SuppliersServiceImpl.
	 *
	 */
	public SuppliersServiceImpl() {
	}

	/**
	 * Save an existing Suppliers entity
	 * 
	 */
	@Transactional
	public void saveSuppliers(Suppliers suppliers) {
		Suppliers existingSuppliers = suppliersDAO.findSuppliersByPrimaryKey(suppliers.getIdsuppliers());

		if (existingSuppliers != null) {
			if (existingSuppliers != suppliers) {
				existingSuppliers.setIdsuppliers(suppliers.getIdsuppliers());
				existingSuppliers.setName(suppliers.getName());
				existingSuppliers.setAddress(suppliers.getAddress());
			}
			suppliers = suppliersDAO.store(existingSuppliers);
		} else {
			suppliers = suppliersDAO.store(suppliers);
		}
		suppliersDAO.flush();
	}

	/**
	 * Delete an existing Suppliers entity
	 * 
	 */
	@Transactional
	public void deleteSuppliers(Suppliers suppliers) {
		suppliersDAO.remove(suppliers);
		suppliersDAO.flush();
	}

	/**
	 * Save an existing SuppliersHasRequests entity
	 * 
	 */
	@Transactional
	public Suppliers saveSuppliersSuppliersHasRequestses(Integer idsuppliers, SuppliersHasRequests related_suppliershasrequestses) {
		Suppliers suppliers = suppliersDAO.findSuppliersByPrimaryKey(idsuppliers, -1, -1);
		SuppliersHasRequests existingsuppliersHasRequestses = suppliersHasRequestsDAO.findSuppliersHasRequestsByPrimaryKey(related_suppliershasrequestses.getSuppliersIdsuppliers(), related_suppliershasrequestses.getRequestsIdrequests());

		// copy into the existing record to preserve existing relationships
		if (existingsuppliersHasRequestses != null) {
			existingsuppliersHasRequestses.setSuppliersIdsuppliers(related_suppliershasrequestses.getSuppliersIdsuppliers());
			existingsuppliersHasRequestses.setRequestsIdrequests(related_suppliershasrequestses.getRequestsIdrequests());
			existingsuppliersHasRequestses.setSenddate(related_suppliershasrequestses.getSenddate());
			existingsuppliersHasRequestses.setPrice(related_suppliershasrequestses.getPrice());
			related_suppliershasrequestses = existingsuppliersHasRequestses;
		}

		related_suppliershasrequestses.setSuppliers(suppliers);
		suppliers.getSuppliersHasRequestses().add(related_suppliershasrequestses);
		related_suppliershasrequestses = suppliersHasRequestsDAO.store(related_suppliershasrequestses);
		suppliersHasRequestsDAO.flush();

		suppliers = suppliersDAO.store(suppliers);
		suppliersDAO.flush();

		return suppliers;
	}

	/**
	 * Delete an existing SuppliersHasRequests entity
	 * 
	 */
	@Transactional
	public Suppliers deleteSuppliersSuppliersHasRequestses(Integer suppliers_idsuppliers, Integer related_suppliershasrequestses_suppliersIdsuppliers, Integer related_suppliershasrequestses_requestsIdrequests) {
		SuppliersHasRequests related_suppliershasrequestses = suppliersHasRequestsDAO.findSuppliersHasRequestsByPrimaryKey(related_suppliershasrequestses_suppliersIdsuppliers, related_suppliershasrequestses_requestsIdrequests, -1, -1);

		Suppliers suppliers = suppliersDAO.findSuppliersByPrimaryKey(suppliers_idsuppliers, -1, -1);

		related_suppliershasrequestses.setSuppliers(null);
		suppliers.getSuppliersHasRequestses().remove(related_suppliershasrequestses);

		suppliersHasRequestsDAO.remove(related_suppliershasrequestses);
		suppliersHasRequestsDAO.flush();

		return suppliers;
	}

	/**
	 */
	@Transactional
	public Suppliers findSuppliersByPrimaryKey(Integer idsuppliers) {
		return suppliersDAO.findSuppliersByPrimaryKey(idsuppliers);
	}

	/**
	 * Return all Suppliers entity
	 * 
	 */
	@Transactional
	public List<Suppliers> findAllSupplierss(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Suppliers>(suppliersDAO.findAllSupplierss(startResult, maxRows));
	}

	/**
	 * Return a count of all Suppliers entity
	 * 
	 */
	@Transactional
	public Integer countSupplierss() {
		return ((Long) suppliersDAO.createQuerySingleResult("select count(o) from Suppliers o").getSingleResult()).intValue();
	}

	/**
	 * Load an existing Suppliers entity
	 * 
	 */
	@Transactional
	public Set<Suppliers> loadSupplierss() {
		return suppliersDAO.findAllSupplierss();
	}
}
